package com.aistar.controller;

import com.aistar.pojo.Traveller;
import com.aistar.service.CommomTravelerService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class CommonTravelersController {

    @Autowired
    CommomTravelerService service;

    /**
     * 跳转到旅客信息页面
     * @return
     */
    @RequestMapping("/travelers")
    public String commonTravele(){
        return "common_traverlers/common_traveler";
    }

    /**
     * 跳转到添加旅客信息页面
     * @return
     */
    @RequestMapping("/travelers_add")
    public String traveleAdd(){
        return "common_traverlers/travellers_add";
    }

    /**
     * 跳转到旅客信息修改页面
     * @return
     */
    @RequestMapping("/travelers_edit")
    public String traveleEdit(){
        return "common_traverlers/travelers_edit";
    }

    /**
     * 根据用户id查询旅客信息
     * @param cusId
     * @return
     */
    @GetMapping("/travelers/customer/{id}")
    @ResponseBody
    public ServerResponse getTravellerInfoByCusId
            (@PathVariable("id")Integer cusId){
        ServerResponse serverResponse = service.getAllTravelersByCusId(cusId);
        return serverResponse;
    }

    /**
     * 添加常用旅客信息
     * 参数是由前端返发送的的 具体见traveller_add.jsp
     * @param userName  旅客名
     * @param tel       电话
     * @param cType     证件类型
     * @param cNo       证件号
     * @param sex       性别
     * @param cusId     属于哪个用户的id （用户id外键）
     * @param tType     旅客类型
     * @return
     */
    @PostMapping("/travelers/add")
    @ResponseBody
    public ServerResponse addTraveller(String userName,String tel,String cType
                                ,String cNo,String sex,String cusId,String tType) {
        Integer customerId = Integer.parseInt(cusId.trim());

        String travellerCardType = "";
        switch (cType){
            case "0" :
                travellerCardType = "身份证";
                break;
            case "1" :
                travellerCardType = "护照";
                break;
            case "2" :
                travellerCardType = "港澳台通行证";
                break;
            case "3" :
                travellerCardType = "其他";
                break;
        }

        String travellerType = "";
        switch (tType){
            case "0" :
                travellerType = "老人";
                break;
            case "1" :
                travellerType = "成人";
                break;
            case "2" :
                travellerType = "儿童";
                break;
        }

        Traveller traveller = new Traveller();
        traveller.setTravellerName(userName);
        traveller.setTravellerTelno(tel);
        traveller.setCertificateType(travellerCardType);
        traveller.setCertificateNo(cNo);
        traveller.setTravellerGender(sex);
        traveller.setCustomerId(customerId);
        traveller.setTravellerType(travellerType);
        traveller.setStatus("1");

        ServerResponse serverResponse = service.addTraveler(traveller);
        return serverResponse;
    }

    /**
     * 根据常用旅客的id查询单个常用旅客信息
     * @param trvId  前端传过来的旅客id
     * @return
     */
    @GetMapping("/travelers/{id}")
    @ResponseBody
    public ServerResponse getTravelerByKey(@PathVariable("id")Integer trvId) {

        ServerResponse serverResponse = service.getTravelerByKey(trvId);
        return serverResponse;
    }

    /**
     * 修改常用旅客信息
     * @param tid  旅客信息id
     * @param cusid  旅客所属用户id
     * @param traname 旅客姓名
     * @param cardtype 证件类型
     * @param cardno 证件号
     * @param tratel 手机号
     * @param tsex  性别
     * @param tratype 旅客类型
     * @return
     */
    @PutMapping("/travelers/update")
    @ResponseBody
    public ServerResponse updateTraveller(String tid,String cusid,String traname,
                                          String cardtype,String cardno,String tratel,
                                          String tsex,String tratype) {

        Integer travelerId = Integer.parseInt(tid.trim());
        Integer customerId = Integer.parseInt(cusid.trim());

        String travellerCardType = "";
        switch (cardtype){
            case "0" :
                travellerCardType = "身份证";
                break;
            case "1" :
                travellerCardType = "护照";
                break;
            case "2" :
                travellerCardType = "港澳台通行证";
                break;
            case "3" :
                travellerCardType = "其他";
                break;
        }

        String travellerType = "";
        switch (tratype){
            case "0" :
                travellerType = "老人";
                break;
            case "1" :
                travellerType = "成人";
                break;
            case "2" :
                travellerType = "儿童";
                break;
        }

        Traveller traveller = new Traveller();
        traveller.setId(travelerId);
        traveller.setCustomerId(customerId);
        traveller.setTravellerName(traname);
        traveller.setCertificateType(travellerCardType);
        traveller.setCertificateNo(cardno);
        traveller.setTravellerTelno(tratel);
        traveller.setTravellerGender(tsex);
        traveller.setTravellerType(travellerType);
        traveller.setStatus("1");

        ServerResponse serverResponse = service.editTraveler(traveller);
        return serverResponse;
    }

    /**
     *删除一个旅客信息 状态为0
     * @param trvId  前端传过来的旅客id
     * @return
     */
    @PutMapping("/travelers/del/{id}")
    @ResponseBody
    public ServerResponse delTraveler(@PathVariable("id")Integer trvId) {

        ServerResponse serverResponse = service.modifyTtavelerDel(trvId);
        return serverResponse;
    }

    /**
     * 根据旅客姓名获取旅客信息
     * @param trvName 前端传过来的旅客姓名
     * @return
     */
    @GetMapping("/travelers/get/{name}")
    @ResponseBody
    public ServerResponse getByTraName(@PathVariable("name")String trvName) {

        ServerResponse serverResponse = service.getTravelerByName(trvName);
        return serverResponse;
    }



}





